package cn.com.yusys.yusp.message.service.message.channel.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.yusys.yusp.message.service.message.channel.SystemSendInterface;
import cn.com.yusys.yusp.message.service.message.channel.impl.DefaultSystemSendImpl;

@Service
public class SystemSendService {
	private Logger logger = LoggerFactory.getLogger(SystemSendService.class);

	@Autowired
	private List<SystemSendInterface> systemSendInterface;
	
	private SystemSendInterface getSend(){
		if(systemSendInterface.size()>1){
			for(SystemSendInterface send:systemSendInterface){
				if(send instanceof DefaultSystemSendImpl){
					
				}else{
					return send;
				}
			}
		}
		return systemSendInterface.get(0);
	}
	
	public int sendMessage(String userNo,String title,String content,Short sendNum){
		int result = 0;;
		try {
			result = getSend().sendMessage(userNo,title, content);
		} catch (Exception e) {
			result = 1;
		}
		
		if (result == 0) {
			return 0;
		} else {
			logger.error("This is the first time that resend system message fails");
		}

		for (int i = 0; i < sendNum; i++) {
			try {
				result = getSend().sendMessage(userNo,title ,content);
			} catch (Exception e) {
				result = 1;
			}
			
			if (result == 0) {
				return 0;
			} else {
				logger.error("This is the [" + (i + 1) + "]th time that resend system message fails");
			}
		}
		return 1;
	}
}